package com.kexio.dynamic.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;
import java.util.Map;

/**
 * Dynamic Starter 配置属性
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
@ConfigurationProperties(prefix = "kexio.dynamic")
public class DynamicProperties {
    
    /**
     * 是否启用Dynamic Starter
     */
    private boolean enabled = true;
    
    /**
     * 动态数据源配置
     */
    private Datasource datasource = new Datasource();
    
    /**
     * 动态权限配置
     */
    private Permission permission = new Permission();
    
    /**
     * 动态配置
     */
    private Config config = new Config();
    
    // Getters and Setters
    public boolean isEnabled() {
        return enabled;
    }
    
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    public Datasource getDatasource() {
        return datasource;
    }
    
    public void setDatasource(Datasource datasource) {
        this.datasource = datasource;
    }
    
    public Permission getPermission() {
        return permission;
    }
    
    public void setPermission(Permission permission) {
        this.permission = permission;
    }
    
    public Config getConfig() {
        return config;
    }
    
    public void setConfig(Config config) {
        this.config = config;
    }
    
    /**
     * 动态数据源配置
     */
    public static class Datasource {
        
        /**
         * 是否启用动态数据源
         */
        private boolean enabled = true;
        
        /**
         * 主数据源名称
         */
        private String primary = "master";
        
        /**
         * 数据源配置
         */
        private Map<String, DataSourceProperty> datasource = new HashMap<>();
        
        // Getters and Setters
        public boolean isEnabled() {
            return enabled;
        }
        
        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
        
        public String getPrimary() {
            return primary;
        }
        
        public void setPrimary(String primary) {
            this.primary = primary;
        }
        
        public Map<String, DataSourceProperty> getDatasource() {
            return datasource;
        }
        
        public void setDatasource(Map<String, DataSourceProperty> datasource) {
            this.datasource = datasource;
        }
        
        /**
         * 数据源属性
         */
        public static class DataSourceProperty {
            
            private String url;
            private String username;
            private String password;
            private String driverClassName;
            
            // Getters and Setters
            public String getUrl() {
                return url;
            }
            
            public void setUrl(String url) {
                this.url = url;
            }
            
            public String getUsername() {
                return username;
            }
            
            public void setUsername(String username) {
                this.username = username;
            }
            
            public String getPassword() {
                return password;
            }
            
            public void setPassword(String password) {
                this.password = password;
            }
            
            public String getDriverClassName() {
                return driverClassName;
            }
            
            public void setDriverClassName(String driverClassName) {
                this.driverClassName = driverClassName;
            }
        }
    }
    
    /**
     * 动态权限配置
     */
    public static class Permission {
        
        /**
         * 权限引擎类型: default, rule-based, script
         */
        private String engine = "default";
        
        /**
         * 是否启用缓存
         */
        private boolean cacheEnabled = true;
        
        /**
         * 缓存过期时间(秒)
         */
        private int cacheTtl = 1800;
        
        // Getters and Setters
        public String getEngine() {
            return engine;
        }
        
        public void setEngine(String engine) {
            this.engine = engine;
        }
        
        public boolean isCacheEnabled() {
            return cacheEnabled;
        }
        
        public void setCacheEnabled(boolean cacheEnabled) {
            this.cacheEnabled = cacheEnabled;
        }
        
        public int getCacheTtl() {
            return cacheTtl;
        }
        
        public void setCacheTtl(int cacheTtl) {
            this.cacheTtl = cacheTtl;
        }
    }
    
    /**
     * 动态配置
     */
    public static class Config {
        
        /**
         * 缓存刷新间隔(秒)
         */
        private int cacheRefreshInterval = 60;
        
        /**
         * 是否启用配置变更监听
         */
        private boolean changeListenerEnabled = true;
        
        // Getters and Setters
        public int getCacheRefreshInterval() {
            return cacheRefreshInterval;
        }
        
        public void setCacheRefreshInterval(int cacheRefreshInterval) {
            this.cacheRefreshInterval = cacheRefreshInterval;
        }
        
        public boolean isChangeListenerEnabled() {
            return changeListenerEnabled;
        }
        
        public void setChangeListenerEnabled(boolean changeListenerEnabled) {
            this.changeListenerEnabled = changeListenerEnabled;
        }
    }
}
